﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Reflection;

namespace AG.Utilities
{
    public enum ProductEdition
    {
        Basic,
        Premium
    }


    public class Licensing
    {
        public static ProductEdition Edition= ProductEdition.Basic;
        
        public static void SaveProductKey (String name, String productKey)
        {
            System.Configuration.Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            oConfig.AppSettings.Settings["ProductKey"].Value = productKey;
            oConfig.AppSettings.Settings["Email"].Value = name;
            oConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        
        public static ProductEdition GetProductEdition(Assembly assembly)
        {
#if DEBUG
            return ProductEdition.Premium;
#endif


            System.Configuration.Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            String productKey = oConfig.AppSettings.Settings["ProductKey"].Value;
            String email = oConfig.AppSettings.Settings["Email"].Value;

            if (IsValidProductKey(email, productKey, assembly))
                return ProductEdition.Premium;
            else
                return ProductEdition.Basic;
        }


        public static Boolean IsValidProductKey(String name, String productKey, Assembly assembly)
        {
            if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(productKey))
            { 
                return false; 
            }
            else
            {
                if(GenerateProductKey(name,assembly) == productKey)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        private static String GenerateProductKey(String name, Assembly assembly)
        {
          
            
            String data = "Q" + (1 / 2).ToString("0.00") + "E" + (1 / 3).ToString("0.00") + "D" + (1 / 3).ToString("0.00") + "B" + (1 / 3).ToString("0.00") + "W" + (1 / 3).ToString("0.00") + "P" + (1 / 3).ToString("0.00") + "D" + (1 / 3).ToString("0.00") + "Q1" + (1 / 3).ToString("0.00") + "E1" + (1 / 3).ToString("0.00") + "D1" + (1 / 3).ToString("0.00") + "B1" + (1 / 3).ToString("0.00") + "W1" + (1 / 3).ToString("0.00") + "P1" + (1 / 3).ToString("0.00") + "D1" + (1 / 3).ToString("0.00") + "Q2" + (1 / 3).ToString("0.00") + "E2" + (1 / 3).ToString("0.00") + "D2" + (1 / 3).ToString("0.00") + "B2" + (1 / 3).ToString("0.00") + "W2" + (1 / 3).ToString("0.00") + "P2" + (1 / 3).ToString("0.00") + "D2" + assembly.GetName().Name + (1 / 3).ToString("0.00") + "_" + (1 / 3).ToString("0.00") + name + (1 / 3).ToString("0.00") + "_" + (1 / 3).ToString("0.00") + assembly.GetName().Version + (1 / 3).ToString("0.00");

          
            byte[] dataBytes = Encoding.UTF8.GetBytes(data.Replace(',', '.'));
            byte[] productKeyCalculated = SHA1CryptoServiceProvider.Create().ComputeHash(dataBytes);

            String productKey = "";

            for (int i = 0; i < 8; i++)
            {
                int mod = i % 2;

                String hexChar = productKeyCalculated[i].ToString("X");

                if (mod == 0 && i != 7 && i != 0)
                {
                    productKey += "-" + hexChar;
                }
                else
                {
                    productKey += hexChar;
                }
            }




            return productKey;
        }
    }
}
